<?php
class Modelrichsnippetsrichsnippets extends Model {

	public function GetCompanyDetails() {

		if(mysql_num_rows(mysql_query("SHOW TABLES LIKE '" . DB_PREFIX . "company_details' "))==1){
				
			$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "company_details WHERE 1");
			return $query->row;
				
		} else{
			return false;
		}

	}

	 
	public function addCompanyDetails($data) {
			
		if(mysql_num_rows(mysql_query("SHOW TABLES LIKE '" . DB_PREFIX . "company_details' "))==1){
			$checkquery = mysql_query("SELECT * FROM " . DB_PREFIX . "company_details WHERE 1");
			$checkdata1=mysql_field_type($checkquery, 5);
			$checkdata2=mysql_field_type($checkquery, 7);

			if($checkdata1=="int"){
				 
				mysql_query("ALTER TABLE " . DB_PREFIX . "company_details CHANGE company_tel company_tel VARCHAR(15)");
				 
			}
			if($checkdata2=="int"){
				 
				mysql_query("ALTER TABLE " . DB_PREFIX . "company_details CHANGE company_postal_code company_postal_code VARCHAR(15)");
	    
			}
			$this->db->query("INSERT INTO `" . DB_PREFIX . "company_details` SET
		                  company_name = '" . $this->db->escape($data['company_name']) . "', 
		                  company_country = '" . $this->db->escape($data['company_country']) . "', 
		                  company_region = '" . $this->db->escape($data['company_region']) . "', 
		                  company_locality = '" . $this->db->escape($data['company_locality']) . "', 
		                  company_postal_code = '" . $this->db->escape($data['company_postal_code']) . "', 
		                  company_address = '" . $this->db->escape($data['company_address']) . "', 
		                  company_tel = '" . $this->db->escape($data['company_tel']) . "', 
		                  company_logo = '" . $this->db->escape($data['company_logo']) . "',
		                  company_email = '" . $this->db->escape($data['company_email']) . "',
		                  company_duns = '" . $data['company_duns'] . "',
		                  company_gln = '" . $data['company_gln'] . "',
		                  company_isicv4 = '" . $data['company_isicv4'] . "',
		                  company_naics = '" . $data['company_naics'] . "',
		                  company_language = '" . $this->db->escape($data['company_language']) . "',
		                  company_lati = '" . $data['company_lati'] . "', 
		                  company_long = '" . $data['company_long'] . "', 
		                  status = '" . (int)$data['status'] . "', 
		                  date_added = NOW()");
			 


		}else{


			$create_company_details = "CREATE TABLE IF NOT EXISTS `" . DB_PREFIX . "company_details` (
		`id` int(11) NOT NULL auto_increment,
		`company_name` varchar(100) collate utf8_bin NOT NULL default '',
		`company_country` varchar(50) collate utf8_bin NOT NULL default '',
        `company_region` varchar(50) collate utf8_bin NOT NULL default '',
        `company_locality` varchar(50) collate utf8_bin NOT NULL default '',
        `company_postal_code` varchar(15) collate utf8_bin NOT NULL default '',
        `company_address` varchar(255) collate utf8_bin NOT NULL default '',
        `company_tel` varchar(15) collate utf8_bin NOT NULL default '',
        `company_logo` varchar(255) collate utf8_bin NOT NULL default '',
        `company_email` varchar(50) collate utf8_bin NOT NULL default '',
        `company_duns` varchar(30) collate utf8_bin NOT NULL default '',
        `company_gln` varchar(30) collate utf8_bin NOT NULL default '',
        `company_isicv4` varchar(30) collate utf8_bin NOT NULL default '',
        `company_naics` varchar(30) collate utf8_bin NOT NULL default '',
        `company_language` varchar(50) collate utf8_bin NOT NULL default '',
        `company_lati` varchar(10) collate utf8_bin NOT NULL default '',
        `company_long` varchar(10) collate utf8_bin NOT NULL default '',
        `status` int(1) NOT NULL default '0',
        `position` int(10) NOT NULL default '0',
        `date_added` datetime default NULL,
        PRIMARY KEY  (`id`)) 
        ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin;";

			$this->db->query($create_company_details);

			$this->db->query("INSERT INTO `" . DB_PREFIX . "company_details` SET
		                  company_name = '" . $this->db->escape($data['company_name']) . "', 
		                  company_country = '" . $this->db->escape($data['company_country']) . "', 
		                  company_region = '" . $this->db->escape($data['company_region']) . "', 
		                  company_locality = '" . $this->db->escape($data['company_locality']) . "', 
		                  company_postal_code = '" . $this->db->escape($data['company_postal_code']) . "', 
		                  company_address = '" . $this->db->escape($data['company_address']) . "', 
		                  company_tel = '" . $this->db->escape($data['company_tel']) . "', 
		                  company_logo = '" . $this->db->escape($data['company_logo']) . "',
		                  company_email = '" . $this->db->escape($data['company_email']) . "',
		                  company_duns = '" . $data['company_duns'] . "',
		                  company_gln = '" . $data['company_gln'] . "',
		                  company_isicv4 = '" . $data['company_isicv4'] . "',
		                  company_naics = '" . $data['company_naics'] . "',
		                  company_language = '" . $this->db->escape($data['company_language']) . "',
		                  company_lati = '" . $data['company_lati'] . "', 
		                  company_long = '" . $data['company_long'] . "', 
		                  status = '" . (int)$data['status'] . "', 
		                  date_added = NOW()");


		}

	}

	public function editCompanyDetails($edit_id, $data) {
		 
		$checkquery = mysql_query("SELECT * FROM " . DB_PREFIX . "company_details WHERE 1");
		$checkdata1=mysql_field_type($checkquery, 5);
		$checkdata2=mysql_field_type($checkquery, 7);

		if($checkdata1=="int"){
			 
			mysql_query("ALTER TABLE " . DB_PREFIX . "company_details CHANGE company_tel company_tel VARCHAR(15)");
	   
		}
		if($checkdata2=="int"){
			 
			mysql_query("ALTER TABLE " . DB_PREFIX . "company_details CHANGE company_postal_code company_postal_code VARCHAR(15)");
			 
		}

		$this->db->query("UPDATE " . DB_PREFIX . "company_details SET
			                  company_name = '" . $this->db->escape($data['company_name']) . "', 
			                  company_country = '" . $this->db->escape($data['company_country']) . "', 
			                  company_region = '" . $this->db->escape($data['company_region']) . "', 
			                  company_locality = '" . $this->db->escape($data['company_locality']) . "', 
			                  company_postal_code = '" . $this->db->escape($data['company_postal_code']) . "', 
			                  company_address = '" . $this->db->escape($data['company_address']) . "', 
			                  company_tel = '" . $this->db->escape($data['company_tel']) . "', 
			                  company_logo = '" . $this->db->escape($data['company_logo']) . "',
			                  company_email = '" . $this->db->escape($data['company_email']) . "',
			                  company_duns = '" . $data['company_duns'] . "',
			                  company_gln = '" . $data['company_gln'] . "',
			                  company_isicv4 = '" . $data['company_isicv4'] . "',
			                  company_naics = '" . $data['company_naics'] . "',
			                  company_language = '" . $this->db->escape($data['company_language']) . "',
			                  company_lati = '" . $data['company_lati'] . "', 
			                  company_long = '" . $data['company_long'] . "',  
			                  status = '" . (int)$data['status'] . "', 
			                  date_added = NOW()
			                  WHERE id = '" . (int)$edit_id . "'");


	}

	public function deleteCompanyDetails($edit_id) {

		$this->db->query("DELETE FROM " . DB_PREFIX . "company_details WHERE id = '" . (int)$edit_id . "'");

	}

	public function GetCompanyDetailsbyId($id) {

		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "company_details WHERE id='".$id."'");
		return $query->row;
	}
}
?>
